const Sequelize = require('sequelize');
module.exports = function (sequelize, DataTypes) {
    return sequelize.define(
        't_employee',
        {
            id: {
                autoIncrement: true,
                type: DataTypes.BIGINT,
                allowNull: false,
                primaryKey: true,
                comment: '员工ID',
            },
            emp_no: {
                type: DataTypes.STRING(50),
                allowNull: false,
                comment: '工号',
                unique: 'emp_no',
            },
            name: {
                type: DataTypes.STRING(50),
                allowNull: false,
                comment: '姓名',
            },
            gender: {
                type: DataTypes.TINYINT,
                allowNull: true,
                comment: '性别（关联字典表）',
            },
            birth_date: {
                type: DataTypes.DATEONLY,
                allowNull: true,
                comment: '出生日期',
            },
            id_card: {
                type: DataTypes.STRING(18),
                allowNull: true,
                comment: '身份证号',
                unique: 'id_card',
            },
            mobile: {
                type: DataTypes.STRING(20),
                allowNull: false,
                comment: '手机号（关联sys_user）',
                unique: 'mobile',
            },
            email: {
                type: DataTypes.STRING(100),
                allowNull: true,
                comment: '邮箱',
            },
            entry_date: {
                type: DataTypes.DATEONLY,
                allowNull: false,
                comment: '入职日期',
            },
            departure_date: {
                type: DataTypes.DATEONLY,
                allowNull: true,
                comment: '离职日期',
            },
            status: {
                type: DataTypes.TINYINT,
                allowNull: false,
                defaultValue: 1,
                comment: '状态（关联字典表）',
            },
            dept_id: {
                type: DataTypes.BIGINT,
                allowNull: true,
                comment: '所属部门ID',
                references: {
                    model: 't_dept',
                    key: 'id',
                },
            },
            position_id: {
                type: DataTypes.BIGINT,
                allowNull: true,
                comment: '职位ID',
                references: {
                    model: 't_position',
                    key: 'id',
                },
            },
            employment_type_id: {
                type: DataTypes.BIGINT,
                allowNull: true,
                comment: '聘用形式ID',
                references: {
                    model: 't_employment_type',
                    key: 'id',
                },
            },
            manager_id: {
                type: DataTypes.BIGINT,
                allowNull: true,
                comment: '直接上级ID（自关联）',
                references: {
                    model: 't_employee',
                    key: 'id',
                },
            },
            emergency_contact: {
                type: DataTypes.STRING(50),
                allowNull: true,
                comment: '紧急联系人',
            },
            emergency_mobile: {
                type: DataTypes.STRING(20),
                allowNull: true,
                comment: '紧急联系人电话',
            },
            create_time: {
                type: DataTypes.DATE,
                allowNull: false,
                defaultValue: Sequelize.Sequelize.literal('CURRENT_TIMESTAMP'),
            },
            update_time: {
                type: DataTypes.DATE,
                allowNull: false,
                defaultValue: Sequelize.Sequelize.literal('CURRENT_TIMESTAMP'),
            },
        },
        {
            sequelize,
            tableName: 't_employee',
            hasTrigger: true,
            timestamps: false,
            indexes: [
                {
                    name: 'PRIMARY',
                    unique: true,
                    using: 'BTREE',
                    fields: [{ name: 'id' }],
                },
                {
                    name: 'emp_no',
                    unique: true,
                    using: 'BTREE',
                    fields: [{ name: 'emp_no' }],
                },
                {
                    name: 'mobile',
                    unique: true,
                    using: 'BTREE',
                    fields: [{ name: 'mobile' }],
                },
                {
                    name: 'id_card',
                    unique: true,
                    using: 'BTREE',
                    fields: [{ name: 'id_card' }],
                },
                {
                    name: 'position_id',
                    using: 'BTREE',
                    fields: [{ name: 'position_id' }],
                },
                {
                    name: 'employment_type_id',
                    using: 'BTREE',
                    fields: [{ name: 'employment_type_id' }],
                },
                {
                    name: 'manager_id',
                    using: 'BTREE',
                    fields: [{ name: 'manager_id' }],
                },
                {
                    name: 'idx_dept',
                    using: 'BTREE',
                    fields: [{ name: 'dept_id' }],
                },
                {
                    name: 'idx_mobile',
                    using: 'BTREE',
                    fields: [{ name: 'mobile' }],
                },
                {
                    name: 'idx_status',
                    using: 'BTREE',
                    fields: [{ name: 'status' }],
                },
            ],
        },
    );
};
